home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gp_vms.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.0 KB  |  448 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gp_vms.c,v 1.4.2.1 2000/11/09 21:13:04 rayjj Exp $ */
  20. /* VAX/VMS specific routines for Ghostscript */
  21. #include "string_.h"
  22. #include "gx.h"
  23. #include "gp.h"
  24. #include "gsstruct.h"
  25. #include <stat.h>
  26. #include <stdlib.h>        /* for exit() with some compiler versions */
  27. #include <errno.h>        /* for exit() with other compiler versions */
  28. #include <unixio.h>
  29.  
  30. extern char *getenv(P1(const char *));
  31.  
  32. /* Apparently gcc doesn't allow extra arguments for fopen: */
  33. #ifdef VMS            /* DEC C */
  34. #  define fopen_VMS fopen
  35. #else /* gcc */
  36. #  define fopen_VMS(name, mode, m1, m2) fopen(name, mode)
  37. #endif
  38.  
  39.  
  40. /* VMS string descriptor structure */
  41. #define DSC$K_DTYPE_T 14
  42. #define DSC$K_CLASS_S  1
  43. struct dsc$descriptor_s {
  44.     unsigned short dsc$w_length;
  45.     unsigned char dsc$b_dtype;
  46.     unsigned char dsc$b_class;
  47.     char *dsc$a_pointer;
  48. };
  49. typedef struct dsc$descriptor_s descrip;
  50.  
  51. /* VMS RMS constants */
  52. #define RMS_IS_ERROR_OR_NMF(rmsv) (((rmsv) & 1) == 0)
  53. #define RMS$_NMF    99018
  54. #define RMS$_NORMAL 65537
  55. #define NAM$C_MAXRSS  255
  56.  
  57. struct file_enum_s {
  58.     uint context, length;
  59.     descrip pattern;
  60.     gs_memory_t *memory;
  61. };
  62. gs_private_st_ptrs1(st_file_enum, struct file_enum_s, "file_enum",
  63.       file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern.dsc$a_pointer);
  64.  
  65. extern uint
  66.     LIB$FIND_FILE(descrip *, descrip *, uint *, descrip *, descrip *,
  67.           uint *, uint *),
  68.     LIB$FIND_FILE_END(uint *),
  69.     SYS$FILESCAN(descrip *, uint *, uint *),
  70.     SYS$PUTMSG(uint *, int (*)(), descrip *, uint);
  71.  
  72. private uint
  73. strlength(char *str, uint maxlen, char term)
  74. {
  75.     uint i = 0;
  76.  
  77.     while (i < maxlen && str[i] != term)
  78.     i++;
  79.     return i;
  80. }
  81.  
  82. /* Do platform-dependent initialization. */
  83. void
  84. gp_init(void)
  85. {
  86. }
  87.  
  88. /* Do platform-dependent cleanup. */
  89. void
  90. gp_exit(int exit_status, int code)
  91. {
  92. }
  93.  
  94. /* Exit the program. */
  95. void
  96. gp_do_exit(int exit_status)
  97. {                /* The program returns exit_status = 0 for OK, 1 for failure; */
  98.     /* VMS has different conventions. */
  99.     switch (exit_status) {
  100.     case 0:
  101.         exit(exit_OK);
  102.     case 1:
  103.         exit(exit_FAILED);
  104.     }
  105.     exit(exit_status);
  106. }
  107.  
  108. /* ------ Date and time ------ */
  109.  
  110. /* Read the current time (in seconds since Jan. 1, 1980) */
  111. /* and fraction (in nanoseconds). */
  112. void
  113. gp_get_realtime(long *pdt)
  114. {
  115.     struct {
  116.     uint _l0, _l1;
  117.     } binary_date, now, difference;
  118.     long lib$ediv(), lib$subx(), sys$bintim(), sys$gettim();
  119.     long units_per_second = 10000000;
  120.     char *jan_1_1980 = "1-JAN-1980 00:00:00.00";
  121.     descrip str_desc;
  122.  
  123.     /* For those interested, Wednesday, November 17, 1858 is the base
  124.        of the Modified Julian Day system adopted by the Smithsonian
  125.        Astrophysical Observatory in 1957 for satellite tracking.  (The
  126.        year 1858 preceded the oldest star catalog in use at the
  127.        observatory.)  VMS uses quadword time stamps which are offsets
  128.        in 100 nanosecond units from November 17, 1858.  With a 63-bit
  129.        absolute time representation (sign bit must be clear), VMS will
  130.        have no trouble with time until 31-JUL-31086 02:48:05.47. */
  131.  
  132.     /* Convert January 1, 1980 into a binary absolute time */
  133.     str_desc.dsc$w_length = strlen(jan_1_1980);
  134.     str_desc.dsc$a_pointer = jan_1_1980;
  135.     (void)sys$bintim(&str_desc, &binary_date);
  136.  
  137.     /* Compute number of 100 nanosecond units since January 1, 1980.  */
  138.     (void)sys$gettim(&now);
  139.     (void)lib$subx(&now, &binary_date, &difference);
  140.  
  141.     /* Convert to seconds and nanoseconds.  */
  142.     (void)lib$ediv(&units_per_second, &difference, &pdt[0], &pdt[1]);
  143.     pdt[1] *= 100;
  144. }
  145.  
  146. /* Read the current user CPU time (in seconds) */
  147. /* and fraction (in nanoseconds).  */
  148. void
  149. gp_get_usertime(long *pdt)
  150. {
  151.     gp_get_realtime(pdt);    /* Use an approximation for now.  */
  152. }
  153.  
  154. /* ------ Screen management ------ */
  155.  
  156. /* Get the environment variable that specifies the display to use. */
  157. const char *
  158. gp_getenv_display(void)
  159. {
  160.     return getenv("DECW$DISPLAY");
  161. }
  162.  
  163. /* ------ Printer accessing ------ */
  164.  
  165. /* Open a connection to a printer.  A null file name means use the */
  166. /* standard printer connected to the machine, if any. */
  167. /* Return NULL if the connection could not be opened. */
  168. FILE *
  169. gp_open_printer(char fname[gp_file_name_sizeof], int binary_mode)
  170. {
  171.     if (strlen(fname) == 0)
  172.     return 0;
  173.     if (binary_mode) {        /*
  174.                  * Printing must be done exactly byte to byte,
  175.                  * using "passall".  However the standard VMS symbiont
  176.                  * does not treat stream-LF files correctly in this respect,
  177.                  * but throws away \n characters.  Giving the file
  178.                  * the record type "undefined", but accessing it as a
  179.                  * normal stream-LF file does the trick.
  180.                  */
  181.     return fopen_VMS(fname, "w", "rfm = udf", "ctx = stm");
  182.     } else {            /* Open as a normal text stream file. */
  183.     return fopen_VMS(fname, "w", "rfm = var", "rat = cr");
  184.     }
  185. }
  186.  
  187. /* Close the connection to the printer. */
  188. void
  189. gp_close_printer(FILE * pfile, const char *fname)
  190. {
  191.     fclose(pfile);
  192. }
  193.  
  194. /* ------ File naming and accessing ------ */
  195.  
  196. /* Define the character used for separating file names in a list. */
  197. const char gp_file_name_list_separator = ',';
  198.  
  199. /* Define the default scratch file name prefix. */
  200. const char gp_scratch_file_name_prefix[] = "_temp_";
  201.  
  202. /* Define the name of the null output file. */
  203. const char gp_null_file_name[] = "NLA0:";
  204.  
  205. /* Define the name that designates the current directory. */
  206. const char gp_current_directory_name[] = "[]";
  207.  
  208. /* Define the string to be concatenated with the file mode */
  209. /* for opening files without end-of-line conversion. */
  210. const char gp_fmode_binary_suffix[] = "";
  211.  
  212. /* Define the file modes for binary reading or writing. */
  213. const char gp_fmode_rb[] = "r";
  214. const char gp_fmode_wb[] = "w";
  215.  
  216. /* Create and open a scratch file with a given name prefix. */
  217. /* Write the actual file name at fname. */
  218. FILE *
  219. gp_open_scratch_file(const char *prefix, char fname[gp_file_name_sizeof],
  220.              const char *mode)
  221. {
  222.     strcpy(fname, prefix);
  223.     strcat(fname, "XXXXXX");
  224.     mktemp(fname);
  225.     return fopen(fname, mode);
  226. }
  227.  
  228. /* Open a file with the given name, as a stream of uninterpreted bytes. */
  229. /* We have to do something special if the file was FTP'ed in binary mode. */
  230. /* Unfortunately, only DEC C supports the extra arguments to fopen. */
  231. FILE *
  232. gp_fopen(const char *fname, const char *mode)
  233. {
  234. #ifdef __DECC
  235. #define FAB$C_FIX 1
  236.     stat_t buffer;
  237.  
  238.     if (stat((char *)fname, &buffer) == 0)
  239.     if (buffer.st_fab_rfm == FAB$C_FIX)
  240.         return fopen(fname, mode, "rfm=stmlf", "ctx=stm");
  241. #endif
  242.     return fopen(fname, mode);
  243. }
  244.  
  245. /* Set a file into binary or text mode. */
  246. int
  247. gp_setmode_binary(FILE * pfile, bool binary)
  248. {
  249.     return 0;            /* Noop under VMS */
  250. }
  251.  
  252. /*  Answer whether a file name contains a directory/device specification, i.e.,
  253.  *  is absolute (not directory- or device-relative).  Since for VMS, the concept
  254.  *  of an "absolute" file reference has no meaning.  As Ghostscript is here
  255.  *  merely checking to see if it will make sense to paste a path to the front
  256.  *  of the file name, we use the VMS system service SYS$FILESCAN to check that
  257.  *  the file name has no node, device, root, or directory specification: if all
  258.  *  four of these items are missing from the file name then it is considered to
  259.  *  a relative file name to which a path may be prefixed. (Roots are associated
  260.  *  with rooted logical names.)
  261.  */
  262.  
  263. bool
  264. gp_file_name_is_absolute(const char *fname, uint len)
  265. {
  266.     descrip str_desc;
  267.  
  268.     /* SYS$FILESCAN takes a uint *, but we want to extract bits. */
  269.     union {
  270.     uint i;
  271.     struct {
  272.         unsigned fscn$v_node:1;
  273.         unsigned fscn$v_device:1;
  274.         unsigned fscn$v_root:1;
  275.         unsigned fscn$v_directory:1;
  276.         unsigned fscn$v_name:1;
  277.         unsigned fscn$v_type:1;
  278.         unsigned fscn$v_version:1;
  279.         unsigned fscn$v_fill_23:1;
  280.     } s;
  281.     } flags;
  282.     uint zero = 0;
  283.  
  284.     str_desc.dsc$w_length = len;
  285.     str_desc.dsc$a_pointer = (char *)fname;
  286.     SYS$FILESCAN(&str_desc, &zero, &flags.i);
  287.     if (flags.s.fscn$v_directory || flags.s.fscn$v_root ||
  288.     flags.s.fscn$v_device || flags.s.fscn$v_node)
  289.     return true;
  290.     else
  291.     return false;
  292. }
  293.  
  294. /* Answer the string to be used for combining a directory/device prefix */
  295. /* with a base file name.  The file name is known to not be absolute. */
  296. const char *
  297. gp_file_name_concat_string(const char *prefix, uint plen,
  298.                const char *fname, uint len)
  299. {
  300.     /*  Full VAX/VMS paths are of the form:
  301.  
  302.      *    device:[root.][directory.subdirectory]filename.extension;version
  303.      *    logical:filename.extension;version
  304.      *
  305.      *  Roots are fairly rare and associated typically with rooted logical
  306.      *  names.
  307.      *
  308.      *  Examples:
  309.      *
  310.      *    DUA1:[GHOSTSCRIPT]GHOST.PS;1
  311.      *    THOR_DEC:[DOOF.A.B.C.D]FILE.DAT;-3
  312.      *    LOG:GHOST.PS  (LOG is a logical defined as DUA1:[GHOSTSCRIPT])
  313.      *    LOG:DOOF.DAT  (LOG is defined as DUA1, current directory is
  314.      *                   is used as the directory spec.)
  315.      *
  316.      */
  317.     if (plen > 0)
  318.     switch (prefix[plen - 1]) {
  319.         case ':':
  320.         case ']':
  321.         return "";
  322.     };
  323.     return ":";
  324. }
  325.  
  326. /* ------ Wild card file search procedures ------ */
  327.  
  328. private void
  329. gp_free_enumeration(file_enum * pfen)
  330. {
  331.     if (pfen) {
  332.     LIB$FIND_FILE_END(&pfen->context);
  333.     gs_free_object(pfen->memory, pfen->pattern.dsc$a_pointer,
  334.                "GP_ENUM(pattern)");
  335.     gs_free_object(pfen->memory, pfen,
  336.                "GP_ENUM(file_enum)");
  337.     }
  338. }
  339.  
  340. /* Begin an enumeration.  See gp.h for details. */
  341.  
  342. file_enum *
  343. gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t * mem)
  344. {
  345.     file_enum *pfen;
  346.     uint i, len;
  347.     char *c, *newpat;
  348.  
  349.     pfen = gs_alloc_struct(mem, file_enum, &st_file_enum,
  350.                "GP_ENUM(file_enum)");
  351.     newpat = (char *)gs_alloc_bytes(mem, patlen, "GP_ENUM(pattern)");
  352.     if (pfen == 0 || newpat == 0) {
  353.     gs_free_object(mem, newpat, "GP_ENUM(pattern)");
  354.     gs_free_object(mem, pfen, "GP_ENUM(file_enum)");
  355.     return (file_enum *) 0;
  356.     }
  357.     /*  Copy the pattern removing backslash quoting characters and
  358.      *  transforming unquoted question marks, '?', to percent signs, '%'.
  359.      *  (VAX/VMS uses the wildcard '%' to represent exactly one character
  360.      *  and '*' to represent zero or more characters.  Any combination and
  361.      *  number of interspersed wildcards is permitted.)
  362.      */
  363.     c = newpat;
  364.     for (i = 0; i < patlen; pat++, i++)
  365.     switch (*pat) {
  366.         case '?':
  367.         *c++ = '%';
  368.         break;
  369.         case '\\':
  370.         i++;
  371.         if (i < patlen)
  372.             *c++ = *++pat;
  373.         break;
  374.         default:
  375.         *c++ = *pat;
  376.         break;
  377.     }
  378.     len = c - newpat;
  379.  
  380.     /* Pattern may not exceed 255 characters */
  381.     if (len > 255) {
  382.     gs_free_object(mem, newpat, "GP_ENUM(pattern)");
  383.     gs_free_object(mem, pfen, "GP_ENUM(file_enum)");
  384.     return (file_enum *) 0;
  385.     }
  386.     pfen->context = 0;
  387.     pfen->length = patlen;
  388.     pfen->pattern.dsc$w_length = len;
  389.     pfen->pattern.dsc$b_dtype = DSC$K_DTYPE_T;
  390.     pfen->pattern.dsc$b_class = DSC$K_CLASS_S;
  391.     pfen->pattern.dsc$a_pointer = newpat;
  392.     pfen->memory = mem;
  393.  
  394.     return pfen;
  395. }
  396.  
  397. /* Return the next file name in the enumeration.  The client passes in */
  398. /* a scratch string and a max length.  If the name of the next file fits, */
  399. /* the procedure returns the length.  If it doesn't fit, the procedure */
  400. /* returns max length +1.  If there are no more files, the procedure */
  401. /* returns -1. */
  402.  
  403. uint
  404. gp_enumerate_files_next(file_enum * pfen, char *ptr, uint maxlen)
  405. {
  406.     char *c, filnam[NAM$C_MAXRSS];
  407.     descrip result =
  408.     {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  409.     uint i, len;
  410.  
  411.     result.dsc$a_pointer = filnam;
  412.  
  413.     /* Find the next file which matches the pattern */
  414.     i = LIB$FIND_FILE(&pfen->pattern, &result, &pfen->context,
  415.               (descrip *) 0, (descrip *) 0, (uint *) 0, (uint *) 0);
  416.  
  417.     /* Check the return status */
  418.     if (RMS_IS_ERROR_OR_NMF(i)) {
  419.     gp_free_enumeration(pfen);
  420.     return (uint)(-1);
  421.     } else if ((len = strlength(filnam, NAM$C_MAXRSS, ' ')) > maxlen)
  422.     return maxlen + 1;
  423.  
  424.     /* Copy the returned filename over to the input string ptr */
  425.     c = ptr;
  426.     for (i = 0; i < len; i++)
  427.     *c++ = filnam[i];
  428.  
  429.     return len;
  430. }
  431.  
  432. /* Clean up a file enumeration.  This is only called to abandon */
  433. /* an enumeration partway through: ...next should do it if there are */
  434. /* no more files to enumerate.  This should deallocate the file_enum */
  435. /* structure and any subsidiary structures, strings, buffers, etc. */
  436.  
  437. void
  438. gp_enumerate_files_close(file_enum * pfen)
  439. {
  440.     gp_free_enumeration(pfen);
  441. }
  442.  
  443. const char *
  444. gp_strerror(int errnum)
  445. {
  446.     return NULL;
  447. }
  448.